from django.shortcuts import render, HttpResponse
from apps.crm.models import User, CourseRecord, JobInfo
import json
import random
from django.db.models import Avg, Count, Max
from apps.crm import forms
# Create your views here.

def test(request):

    #b = [{'a':1, 'b':2}, {'a':1, 'b':2}, {'a':1, 'b':2}, {'a':1, 'b':2}, {'a':1, 'b':2}]
    # [{"name":'day', "data":[1,1,1,1,1]},{"name":"pv", "data":[1,1,1,1,1]}]
    b = [{"name": 'day', "data": [random.randint(1,5) for i  in range(10)]}, {"name": "PV", "data": [random.randint(4,8) for i  in range(10)]},
         {"name": "UV", "data": [random.randint(6,10) for i  in range(10)]}]
    return HttpResponse(json.dumps(b))
def job_count(request):
    b =  [{
        "type": 'pie',
        "name": '2017 就业信息占比',
        "data": [
            ['3000-6000', JobInfo.objects.filter(salary__gte=3000, salary__lte=6000).count()],
            ['6000-8000', JobInfo.objects.filter(salary__gte=6000, salary__lte=8000).count()],
            ['<3000', JobInfo.objects.filter(salary__lte=3000).count()],
            ['>8000', JobInfo.objects.filter(salary__gte=8000).count()],

            ]
        }]

    return HttpResponse(json.dumps(b))

# def test(request):
#     username = request.GET.get('user', 'NO User')
#     b = {'code':200, 'message':username+'用户创建成功'}
#     #b = [{'a':1, 'b':2}, {'a':1, 'b':2}, {'a':1, 'b':2}, {'a':1, 'b':2}, {'a':1, 'b':2}]
#     # [{"name":'day', "data":[1,1,1,1,1]},{"name":"pv", "data":[1,1,1,1,1]}]
#     # b = [{"name": 'day', "data": [random.randint(1,5) for i  in range(10)]}, {"name": "PV", "data": [random.randint(4,8) for i  in range(10)]},
#     #      {"name": "UV", "data": [random.randint(6,10) for i  in range(10)]}]
#     return HttpResponse(json.dumps(b))
def job_info(request):
    job_info = JobInfo.objects.values('student__detail__real_name','company', 'salary','job_photo','city','position')

    # kwgs = {
    #         'job_info':job_info
    # }
    # job_info = [{1:1},{1:1}]
    info_list = list(job_info)
    for i in info_list:
        i['job_photo'] = '<img src="/media/{}" style="width:100px;height:100px">'.format(i['job_photo'])
    print(info_list)
    server_info_list = {'total':10, 'rows':info_list}
    return HttpResponse(json.dumps(server_info_list))

def course_record(request, id):
    #form = forms.HomworkForm()
    course_record = CourseRecord.objects.get(id=id)
    kwgs = {#form":form,
            'course_record':course_record
            }

    return render(request, 'comm/course_record.html', kwgs)